<?php

/**
 *
 *	@author Smarttang <[<yucong.tang@zhaopin.com.cn>]>
 *	@date(2017.8.20) 创建
 *	@updatetime(2017.8.20) 创建滞后跟踪添加事件
 * 
 */
class Order_model extends CI_Model{

	function __construct()
	{
		parent::__construct();
		$this->load->database();
		$this->load->helper('date');
		$this->load->library('session');
		$this->load->library('cemail');
	}

	/**
	 * 获得滞后事件的列表
	 * @return [type] [description]
	 */
	function behind_list($inputs)
	{
		$this->db->select('ZL_BEHINDS_ID');
		$zl_behind_count = $this->db->get('zl_behinds')->num_rows();

		$this->db->select('ZL_BEHINDS_HASHID behind_hashid,ZL_BEHINDS_ID behind_id,ZL_BEHINDS_TITLE benhind_name,ZL_BEHINDS_LEVEL benhind_level,appealer.ZL_USER_NAME behind_appealer,sec_appealer.ZL_USER_NAME behind_sec_principal,ZL_BEHINDS_STARTTIME behind_starttime,ZL_BEHINDS_STATUS status');
		$this->db->join('zl_users as appealer', 'zl_behinds.ZL_BEHINDS_APPEALER = appealer.ZL_USER_HASHID','left');
		$this->db->join('zl_users as sec_appealer', 'zl_behinds.ZL_BEHINDS_SEC_PRINCIPAL = sec_appealer.ZL_USER_HASHID','left');
		$this->db->limit($inputs['limit'],$inputs['offset']);
		$zl_behind = $this->db->get('zl_behinds');

		return array(
			'status' => 1,
			'data' => $zl_behind->result_array(),
			'total' => $zl_behind_count,
		);
	}

		/**
	 * 滞后事件单独读取
	 * @param  [type] $inputs [description]
	 * @return [type]         [description]
	 */
	function behind_read($inputs)
	{
		$this->db->where('ZL_BEHINDS_HASHID',$inputs['id']);
		$this->db->select('ZL_BEHINDS_ID behind_id,ZL_BEHINDS_TITLE behind_name,appealer.ZL_USER_NAME behind_appealer,sec_principal.ZL_USER_NAME behind_sec_appealer,check_principal.ZL_USER_NAME behind_check_appealer,ZL_BEHINDS_STARTTIME behind_starttime');
		$this->db->join('zl_users as appealer', 'zl_behinds.ZL_BEHINDS_APPEALER = appealer.ZL_USER_HASHID','left');
		$this->db->join('zl_users as sec_principal', 'zl_behinds.ZL_BEHINDS_SEC_PRINCIPAL = sec_principal.ZL_USER_HASHID','left');
		$this->db->join('zl_users as check_principal', 'zl_behinds.ZL_BEHINDS_CHECK_PRINCIPAL = check_principal.ZL_USER_HASHID','left');
		$behind_data = $this->db->get('zl_behinds');

		if ($behind_data->num_rows() < 1){
			return array('status' => 0);
		}

		$this->db->where('ZL_BEHINDS_HASHID',$inputs['id']);
		$this->db->select('ZL_BEHINDS_CONTENT behind_content,ZL_BEHINDS_APPEAL_CONTENT behind_appealer_content');
		$behind_content_data = $this->db->get('zl_behind_content');

		return array('status' => 1, 'data' => array_merge($behind_data->result_array(),$behind_content_data->result_array()));
	}


	/**
	 * 滞后事件读取待编辑内容
	 * @param  [type] $inputs [description]
	 * @return [type]         [description]
	 */
	function behind_change($inputs)
	{
		$this->db->where('ZL_BEHINDS_HASHID',$inputs['id']);
		$this->db->select('ZL_BEHINDS_ID behind_id,ZL_BEHINDS_TITLE behind_name,ZL_BEHINDS_LEVEL behind_level,appealer.ZL_USER_NAME behind_appealer,sec_principal.ZL_USER_NAME behind_sec_appealer,check_principal.ZL_USER_NAME behind_check_appealer,ZL_BEHINDS_STARTTIME behind_starttime');
		$this->db->join('zl_users as appealer', 'zl_behinds.ZL_BEHINDS_APPEALER = appealer.ZL_USER_HASHID','left');
		$this->db->join('zl_users as sec_principal', 'zl_behinds.ZL_BEHINDS_SEC_PRINCIPAL = sec_principal.ZL_USER_HASHID','left');
		$this->db->join('zl_users as check_principal', 'zl_behinds.ZL_BEHINDS_CHECK_PRINCIPAL = check_principal.ZL_USER_HASHID','left');
		$behind_data = $this->db->get('zl_behinds');

		if ($behind_data->num_rows() < 1){
			return array('status' => 0);
		}

		$this->db->where('ZL_BEHINDS_HASHID',$inputs['id']);
		$this->db->select('ZL_BEHINDS_CONTENT behind_content,ZL_BEHINDS_APPEAL_CONTENT behind_appealer_content');
		$behind_content_data = $this->db->get('zl_behind_content');

		return array('status' => 1, 'data' => array_merge($behind_data->result_array(),$behind_content_data->result_array()));
	}


	/**
	 * 确认修改指定用户组信息
	 * @param  [type] $inputs [description]
	 * @return [type]         [description]
	 * @author xiaoting 
	 */
	function behind_surechange($inputs)
	{
		$behind_data = array(
			'ZL_BEHINDS_TITLE' => $inputs['behind_name'],
			//'ZL_BEHINDS_LEVEL' => intval($inputs['behind_level']),
			'ZL_BEHINDS_APPEALER' => $inputs['behind_appealer'],
			'ZL_BEHINDS_SEC_PRINCIPAL' => $inputs['behind_sec_principal'],
			'ZL_BEHINDS_CHECK_PRINCIPAL' => $inputs['behind_check_principal'],
		);

		$behind_content = array(
			'ZL_BEHINDS_CONTENT' => $inputs['behind_content'],
			'ZL_BEHINDS_APPEAL_CONTENT' => $inputs['behind_appealer_content']
		);

		$this->db->trans_begin();
		$this->db->where('ZL_BEHINDS_ID', $inputs['behind_id']);
		$return_data = $this->db->update('zl_behinds',$behind_data);
	
		
		$this->db->where('ZL_BEHINDS_ID', $inputs['behind_id']);
		$this->db->select('ZL_BEHINDS_HASHID behind_hashid');
		$query = $this->db->get('zl_behinds')->result();
		$behind_hashid = '';
		foreach ($query as $row)
		{
		    $behind_hashid = $row->behind_hashid;
		}
		$this->db->where('ZL_BEHINDS_HASHID',$behind_hashid);
		$return_content = $this->db->update('zl_behind_content', $behind_content);
		if ($return_data && $return_content){
			$this->db->trans_commit();
			return array('status' => 1);
		}
		else{
			$this->db->trans_rollback();
			return array('status' => 0);
		}
	}

	/**
	 * 删除滞后事件
	 * @param  [type] $inputs [description]
	 * @return [type]         [description]
	 */
	function behind_delete($inputs)
	{
		// 查找是否存在
		$this->db->where('ZL_BEHINDS_HASHID',$inputs['id']);
		$this->db->select('ZL_BEHINDS_ID');
		$zl_behind_count = $this->db->get('zl_behinds')->num_rows();

		if ($zl_behind_count < 1){
			return array('status' => 0);
		}

		$delete_array = array(
			'zl_behinds' => -1,
			'zl_behind_content' => -2,
			'zl_behind_checkinfo' => -3
		);

		foreach ($delete_array as $key => $value) {
			// 删除数据
			$this->db->where('ZL_BEHINDS_HASHID',$inputs['id']);
			$delete = $this->db->delete($key);

			if (!$delete){
				return array('status' => $value);
			}
		}

		return array('status' => 1);

	}

	/**
	 * 添加一个滞后跟踪事项
	 * @param  [type] $inputs [description]
	 * @return [type]         [description]
	 */
	function behind_add($inputs)
	{
		$id = 'ZL-BEHINDS-'. date("Ymd-His");
		// 基础数据
		$behind_data = array(
			'ZL_BEHINDS_ID' => $id,
			'ZL_BEHINDS_TITLE' => $inputs['behind_name'],
			'ZL_BEHINDS_LEVEL' => intval($inputs['behind_level']),
			'ZL_BEHINDS_APPEALER' => $inputs['behind_appealer'],
			'ZL_BEHINDS_STARTTIME' => $this->comon->_datetime(),
			'ZL_BEHINDS_SEC_PRINCIPAL' => $inputs['behind_sec_principal'],
			'ZL_BEHINDS_CHECK_PRINCIPAL' => $inputs['behind_check_principal'],
			'ZL_BEHINDS_STATUS' => 0,
			'ZL_BEHINDS_HASHID' => $this->comon->_createRomdomKey()
		);

		// 内容信息
		$behind_content = array(
			'ZL_BEHINDS_HASHID' => $behind_data['ZL_BEHINDS_HASHID'],
			'ZL_BEHINDS_CONTENT' => $inputs['behind_content'],
			'ZL_BEHINDS_APPEAL_CONTENT' => $inputs['behind_appeal_content']
		);

		$behind_data_status = $this->db->insert('zl_behinds',$behind_data);

		if(!$behind_data_status){
			return array('status' => 0);
		}

		$behind_content_status = $this->db->insert('zl_behind_content',$behind_content);

		if (!$behind_content_status){
			return array('status' => -1);
		}
		return array('status' => 1);
	}

	/**
	 * 读取待推进滞后事项
	 * @param  [type] $inputs [description]
	 * @return [type]         [description]
	 */
	function behind_push($inputs)
	{
		$this->db->where('ZL_BEHINDS_HASHID',$inputs['id']);
		$this->db->select('ZL_BEHINDS_CHECK_TIME behind_push_time,ZL_BEHINDS_CHECK_CONTENT behind_push_content');
		$behind_data = $this->db->get('zl_behind_checkinfo');

		return array(
			'status' => 1, 
			'data' => $behind_data->result_array()
		);
	}

	/**
	 * 确认推进滞后事项
	 * @param  [type] $inputs [description]
	 * @return [type]         [description]
	 */
	function behind_surepush($inputs)
	{
		// 基础数据
		$behind_add_data = array(
			'ZL_BEHINDS_HASHID' => $inputs['behind_hashid'],
			'ZL_BEHINDS_CHECK_TIME' => $this->comon->_datetime(),
			'ZL_BEHINDS_CHECK_CONTENT' => $inputs['behind_push_content']
		);

		$behind_update_data = array(
			'ZL_BEHINDS_CHECK_TIME' => $this->comon->_datetime(),
			'ZL_BEHINDS_CHECK_CONTENT' => $inputs['behind_push_content']
		);

		$this->db->where('ZL_BEHINDS_HASHID', $inputs['behind_hashid']);
		$row = $this->db->get('zl_behind_checkinfo')->num_rows();

		if($row){
			$this->db->where('ZL_BEHINDS_HASHID',$inputs['behind_hashid']);
			$behind_content_status = $this->db->update('zl_behind_checkinfo', $behind_update_data);
		}
		else{
			$behind_content_status = $this->db->insert('zl_behind_checkinfo',$behind_add_data);
		}
		if (!$behind_content_status){
			return array('status' => -1);
		}
		return array('status' => 1);
	}	


	/**
	 * 获得SEC事件的列表
	 * @return [type] [description]
	 */
	function src_list($inputs)
	{

		$this->db->select('ZL_SRC_ID');
		$zl_src_count = $this->db->get('zl_src')->num_rows();

		$this->db->select('ZL_SRC_ID id,ZL_SRC_TIME time,ZL_SRC_NAME name,ZL_SRC_TYPE  type,ZL_SRC_PUSHUSER pushuser,ZL_SRC_DEALUSER dealuser,ZL_SRC_STATUS status');
		$this->db->limit($inputs['limit'],$inputs['offset']);
		$zl_src = $this->db->get('zl_src');

		return array(
			'status' => 1,
			'data' => $zl_src->result_array(),
			'total' => $zl_src_count
		);
	}

	/**
	 * 添加一个滞后跟踪事项
	 * @param  [type] $inputs [description]
	 * @return [type]         [description]
	 */
	function src_add($inputs)
	{
		$id = 'ZL-SRC-'. date("Ymd-His");
		$session =  $this->session->userdata('username');
		// 基础数据
		$zl_src_data = array(
			'ZL_SRC_ID' => $id,
			'ZL_SRC_TIME' => $this->comon->_datetime(),
			'ZL_SRC_NAME' => $inputs['name'],
			'ZL_SRC_TYPE' => $inputs['src_type'],
			'ZL_SRC_PUSHUSER' => $session,
			'ZL_SRC_STATUS' => 1
		);
		$zl_src_process_data = array(
			'ZL_SRC_PROCESS_SRCID' =>  $id,
			'ZL_SRC_PROCESS_TIME' => $this->comon->_datetime(),
			'ZL_SRC_PROCESS_USER' => $session,
			'ZL_SRC_CONTENT_APPEAL' => '申请人提交工单，等待安全指派处理人'
		);
		switch ($inputs['src_type']) {
			case '1':
				$zl_src_attr_content_data = array(
					array(
						'ZL_SRC_ATTR_CONTENT_SRCID' => $id,
						'ZL_SRC_ATTR_CONTENT_KEY' => '异常详情',
						'ZL_SRC_ATTR_CONTENT_VALUE' => $inputs['content']
					),
					array(
						'ZL_SRC_ATTR_CONTENT_SRCID' => $id,
						'ZL_SRC_ATTR_CONTENT_KEY' => '最终诉求',
						'ZL_SRC_ATTR_CONTENT_VALUE' => $inputs['result']
					)
				);
				$this->db->trans_begin();
				$zl_src = $this->db->insert('zl_src',$zl_src_data);
				$zl_src_attr_content = $this->db->insert_batch('zl_src_attr_content',$zl_src_attr_content_data);
				$zl_src_process = $this->db->insert('zl_src_process',$zl_src_process_data);

				if ($zl_src && $zl_src_attr_content && $zl_src_process){
					$this->db->trans_commit();
				}
				else{
				    $this->db->trans_rollback();
				     return array('status' => -1);
				}
				break;

			case '2':
				$zl_src_attr_content_data = array(
					array(
						'ZL_SRC_ATTR_CONTENT_SRCID' => $id,
						'ZL_SRC_ATTR_CONTENT_KEY' => '异常详情',
						'ZL_SRC_ATTR_CONTENT_VALUE' => $inputs['content']
					),
					array(
						'ZL_SRC_ATTR_CONTENT_SRCID' => $id,
						'ZL_SRC_ATTR_CONTENT_KEY' => '最终诉求',
						'ZL_SRC_ATTR_CONTENT_VALUE' => $inputs['result']
					)
				);
				$this->db->trans_begin();
				$zl_src = $this->db->insert('zl_src',$zl_src_data);
				$zl_src_attr_content = $this->db->insert_batch('zl_src_attr_content',$zl_src_attr_content_data);
				$zl_src_process = $this->db->insert('zl_src_process',$zl_src_process_data);

				if ($zl_src && $zl_src_attr_content && $zl_src_process){
					$this->db->trans_commit();
				}
				else{
				    $this->db->trans_rollback();
				     return array('status' => -1);
				}

				break;

			case '3':
				$zl_src_attr_content_data = array(
					array(
						'ZL_SRC_ATTR_CONTENT_SRCID' => $id,
						'ZL_SRC_ATTR_CONTENT_KEY' => '异常详情',
						'ZL_SRC_ATTR_CONTENT_VALUE' => $inputs['content']
					),
					array(
						'ZL_SRC_ATTR_CONTENT_SRCID' => $id,
						'ZL_SRC_ATTR_CONTENT_KEY' => '最终诉求',
						'ZL_SRC_ATTR_CONTENT_VALUE' => $inputs['result']
					)
				);
				$this->db->trans_begin();
				$zl_src = $this->db->insert('zl_src',$zl_src_data);
				$zl_src_attr_content = $this->db->insert_batch('zl_src_attr_content',$zl_src_attr_content_data);
				$zl_src_process = $this->db->insert('zl_src_process',$zl_src_process_data);

				if ($zl_src && $zl_src_attr_content && $zl_src_process){
					$this->db->trans_commit();
				}
				else{
				    $this->db->trans_rollback();
				     return array('status' => -1);
				}
				break;

			case '4':
				$zl_src_attr_data = array(
					array(
						'ZL_SRC_ATTR_SRCID' => $id,
						'ZL_SRC_ATTR_KEY' => '企业ID',
						'ZL_SRC_ATTR_VALUE' => $inputs['enterpriseID']
					),
					array(
						'ZL_SRC_ATTR_SRCID' => $id,
						'ZL_SRC_ATTR_KEY' => '企业名称',
						'ZL_SRC_ATTR_VALUE' => $inputs['enterpriseName']
					),
					array(
						'ZL_SRC_ATTR_SRCID' => $id,
						'ZL_SRC_ATTR_KEY' => '企业常用IP地址',
						'ZL_SRC_ATTR_VALUE' => $inputs['enterpriseIP']
					),
					array(
						'ZL_SRC_ATTR_SRCID' => $id,
						'ZL_SRC_ATTR_KEY' => '日常使用时间',
						'ZL_SRC_ATTR_VALUE' => $inputs['time']
					),
					array(
						'ZL_SRC_ATTR_SRCID' => $id,
						'ZL_SRC_ATTR_KEY' => '每天访问简历数量',
						'ZL_SRC_ATTR_VALUE' => $inputs['visitNumber']
					),
					array(
						'ZL_SRC_ATTR_SRCID' => $id,
						'ZL_SRC_ATTR_KEY' => '账号是否为管理员',
						'ZL_SRC_ATTR_VALUE' => $inputs['isAdmin']
					),
					array(
						'ZL_SRC_ATTR_SRCID' => $id,
						'ZL_SRC_ATTR_KEY' => '状态异常账号名称',
						'ZL_SRC_ATTR_VALUE' => $inputs['account']
					),
					array(
						'ZL_SRC_ATTR_SRCID' => $id,
						'ZL_SRC_ATTR_KEY' => '联系方式',
						'ZL_SRC_ATTR_VALUE' => $inputs['contact']
					),
					array(
						'ZL_SRC_ATTR_SRCID' => $id,
						'ZL_SRC_ATTR_KEY' => '账号使用人数',
						'ZL_SRC_ATTR_VALUE' => $inputs['useNumber']
					),
					array(
						'ZL_SRC_ATTR_SRCID' => $id,
						'ZL_SRC_ATTR_KEY' => '账号绑定',
						'ZL_SRC_ATTR_VALUE' => $inputs['binding']
					)
				);

				$zl_src_attr_content_data = array(
					array(
						'ZL_SRC_ATTR_CONTENT_SRCID' => $id,
						'ZL_SRC_ATTR_CONTENT_KEY' => '异常详情',
						'ZL_SRC_ATTR_CONTENT_VALUE' => $inputs['content']
					),
					array(
						'ZL_SRC_ATTR_CONTENT_SRCID' => $id,
						'ZL_SRC_ATTR_CONTENT_KEY' => '最终诉求',
						'ZL_SRC_ATTR_CONTENT_VALUE' => $inputs['result']
					)
				);

				$this->db->trans_begin();
				$zl_src = $this->db->insert('zl_src',$zl_src_data);
				$zl_src_attr = $this->db->insert_batch('zl_src_attr',$zl_src_attr_data);
				$zl_src_attr_content = $this->db->insert_batch('zl_src_attr_content',$zl_src_attr_content_data);
				$zl_src_process = $this->db->insert('zl_src_process',$zl_src_process_data);

				if ($zl_src && $zl_src_attr && $zl_src_attr_content && $zl_src_process){
					$this->db->trans_commit();
				}
				else{
				    $this->db->trans_rollback();
				     return array('status' => -1);
				}
				break;

			case '5':
				$zl_src_attr_content_data = array(
					array(
						'ZL_SRC_ATTR_CONTENT_SRCID' => $id,
						'ZL_SRC_ATTR_CONTENT_KEY' => '异常详情',
						'ZL_SRC_ATTR_CONTENT_VALUE' => $inputs['content']
					),
					array(
						'ZL_SRC_ATTR_CONTENT_SRCID' => $id,
						'ZL_SRC_ATTR_CONTENT_KEY' => '最终诉求',
						'ZL_SRC_ATTR_CONTENT_VALUE' => $inputs['result']
					)
				);
				$this->db->trans_begin();
				$zl_src = $this->db->insert('zl_src',$zl_src_data);
				$zl_src_attr_content = $this->db->insert_batch('zl_src_attr_content',$zl_src_attr_content_data);
				$zl_src_process = $this->db->insert('zl_src_process',$zl_src_process_data);

				if ($zl_src &&  $zl_src_attr_content && $zl_src_process){
					$this->db->trans_commit();
				}
				else{
				    $this->db->trans_rollback();
				     return array('status' => -1);
				}
				break;

			case '6':
				$zl_src_attr_content_data = array(
					array(
						'ZL_SRC_ATTR_CONTENT_SRCID' => $id,
						'ZL_SRC_ATTR_CONTENT_KEY' => '异常详情',
						'ZL_SRC_ATTR_CONTENT_VALUE' => $inputs['content']
					),
					array(
						'ZL_SRC_ATTR_CONTENT_SRCID' => $id,
						'ZL_SRC_ATTR_CONTENT_KEY' => '最终诉求',
						'ZL_SRC_ATTR_CONTENT_VALUE' => $inputs['result']
					)
				);
				$this->db->trans_begin();
				$zl_src = $this->db->insert('zl_src',$zl_src_data);
				$zl_src_attr_content = $this->db->insert_batch('zl_src_attr_content',$zl_src_attr_content_data);
				$zl_src_process = $this->db->insert('zl_src_process',$zl_src_process_data);

				if ($zl_src && $zl_src_attr_content && $zl_src_process){
					$this->db->trans_commit();
					 return array('status' => 1);
				}
				else{
				    $this->db->trans_rollback();
				     return array('status' => -1);
				}
				break;
			
			default:
				# code...
				break;
		}

		$to ="src@zhaopin.com.cn";
		$cc = "";

		$title = 'SRC事件 - 有新申请的工单';
		$message = '见信好，申请人（'.$session.'）提交工单，等待安全指派处理人';
		
		$url_address = 'http://'.$_SERVER['HTTP_HOST'].base_url('index.php/main?path=src');
		$button = '<a class="my-btn" href="'.$url_address.'" style="width:150px;">去安全平台看看</a>';
		$email_items = array(
			'SRC事件ID' => $id, 
			'工单名称' => $inputs['name'],
			'状态' => "申请人提交工单，等待安全指派处理人"
		);
		$to = "pengqiao.geng@zhaopin.com.cn";
		$cc = "kaige.dong@zhaopin.com.cn";
		$emailHtml = $this->cemail->createHtml($email_items,$message,$button);
		if(!$this->comon_model->_sendEmail($to,$cc,'[智联安全运维平台]'.$title,$emailHtml)){
			return array('status' => -3);
		}else{
			return array('status' => 1);
		}
		
	}

		/**
	 * 获得SEC事件推进流程
	 * @return [type] [description]
	 */
	function src_push($inputs)
	{
		$this->db->select('ZL_SRC_STATUS ,ZL_SRC_PUSHUSER,ZL_SRC_DEALUSER,ZL_SRC_ASSIGNUSER,ZL_SRC_NAME');
		$this->db->where('ZL_SRC_ID',$inputs['id']);
		$zl_src = $this->db->get('zl_src');

		$pushuser = $zl_src->row()->ZL_SRC_PUSHUSER;
		$dealuser = $zl_src->row()->ZL_SRC_DEALUSER;
		$assiuser = $zl_src->row()->ZL_SRC_ASSIGNUSER;
		$session =  strtolower($this->session->userdata('username'));

		$this->db->select('ZL_USER_EMAIL');
		$this->db->where('ZL_USER_NAME',$pushuser);
		$zl_users = $this->db->get('zl_users');
		$push_mail = $zl_users->row()->ZL_USER_EMAIL;

		$this->db->select('ZL_USER_EMAIL');
		$this->db->where('ZL_USER_NAME',$assiuser);
		$zl_users = $this->db->get('zl_users');
		$assign_mail = $zl_users->row()->ZL_USER_EMAIL;

		$this->db->select('ZL_USER_EMAIL');
		$this->db->where('ZL_USER_NAME',$pushuser);
		$zl_users = $this->db->get('zl_users');
		$deal_mail = $zl_users->row()->ZL_USER_EMAIL;


		$data = array(
			'ZL_SRC_TIME' => $this->comon->_datetime()
		);
		$data_process = array(
			'ZL_SRC_PROCESS_TIME' => $this->comon->_datetime(),
			'ZL_SRC_PROCESS_SRCID' => $inputs['id'],
			'ZL_SRC_PROCESS_USER' => $session
		);

		$cc = '';
		$to ='';
		$title = 'SRC事件 - 推进';
		
		$url_address = 'http://'.$_SERVER['HTTP_HOST'].base_url('index.php/main?path=src');
		$button = '<a class="my-btn" href="'.$url_address.'" style="width:150px;">去平台看看</a>';
		if ($inputs['remarks'] == null) {
			$inputs['remarks'] = "无";
		}
		$email_items = array(
			'SRC事件ID' => $inputs['id'],
			'工单名称' => $zl_src->row()->ZL_SRC_NAME
		);
		switch ($zl_src->row()->ZL_SRC_STATUS) {
			case '1':
				return array('status' => -6);
				break;

			case '2':
				if ($zl_src->row()->ZL_SRC_DEALUSER != $session) {
					 return array('status' => -3);
				}
				$data['ZL_SRC_STATUS'] = 3 ;
				$data_process['ZL_SRC_PROCESS_USER'] = $session;
				$data_process['ZL_SRC_CONTENT_APPEAL'] = '处理人（'.$session.'）接收，等待处理人（'.$session.'）解决</br>来自处理人（'.$session.'）的备注'.$inputs['remarks'];

				$message = '见信好，处理人（'.$session.'）接收，等待处理人（'.$session.'）解决';
				$email_items['状态'] = "安全已指派处理人，等待处理人接受";
				$email_items['来自指派人（'.$session.'）的备注'] = $inputs['remarks'];

				$to = $deal_mail.",".$assign_mail;
				$cc = $push_mail;
				break;

			case '3':
				if ($zl_src->row()->ZL_SRC_DEALUSER != $session) {
					 return array('status' => -4);
				}
				$data['ZL_SRC_STATUS'] = 4 ;
				$data_process['ZL_SRC_PROCESS_USER'] = $session;
				$data_process['ZL_SRC_CONTENT_APPEAL'] = '处理人（'.$session.'已解决，等待申请人（'.$pushuser.'）确认</br>来自处理人（'.$session.'）的备注'.$inputs['remarks'];

				$message = '见信好，处理人（'.$session.'）已解决，等待申请人（'.$pushuser.'）确认';
				$email_items['状态'] = "处理人已解决，等待申请人确认";
				$email_items['来自处理人（'.$session.'）的备注'] = $inputs['remarks'];
				

				$to = $deal_mail.",".$push_mail;
				$cc = $assign_mail;

				break;

			case '4':
				if ($zl_src->row()->ZL_SRC_PUSHUSER != $session) {
					 return array('status' => -3);
				}
				if ($inputs['isOK']=='1') {
					$data['ZL_SRC_STATUS'] = 5 ;
					$data_process['ZL_SRC_PROCESS_USER'] = $session;
					$data_process['ZL_SRC_CONTENT_APPEAL'] = '申请人（'.$pushuser.'）已确认，工单完成</br>来自申请人（'.$session.'）的备注'.$inputs['remarks'];

					$message = '见信好，申请人（'.$pushuser.'）已确认，工单完成';
					$email_items['状态'] = "申请人已确认，工单完成";
					$email_items['来自申请人（'.$session.'）的备注'] = $inputs['remarks'];

				}else{
					$data['ZL_SRC_STATUS'] = 3 ;
					$data_process['ZL_SRC_PROCESS_USER'] = $session;
					$data_process['ZL_SRC_CONTENT_APPEAL'] = '申请人（'.$pushuser.'）驳回，等待处理人（'.$dealuser.'）解决</br>来自申请人（'.$session.'）的备注'.$inputs['remarks'];
					
					$message = '见信好，申请人（'.$pushuser.'）驳回，等待处理人（'.$dealuser.'）解决';
					$email_items['状态'] = "申请人驳回，等待处理人解决";
					$email_items['来自申请人（'.$session.'）的备注'] = $inputs['remarks'];
				}
				
				if ($zl_src->row()->ZL_SRC_DEALUSER != $session) {
					 return array('status' => -4);
				}

				$to = $deal_mail.",".$push_mail;
				$cc = $assign_mail;

				break;

			case '5':
				return array('status' => -5);
				break;
			
			default:
				# code...
				break;
		}

		$this->db->trans_begin();
		$this->db->where('ZL_SRC_ID',$inputs['id']);
		$zl_src_update = $this->db->update('zl_src',$data);
		$zl_src_process = $this->db->insert('zl_src_process',$data_process);

		if ($zl_src_update && $zl_src_process){
			$this->db->trans_commit();
			//发送邮件
			$to = "pengqiao.geng@zhaopin.com.cn";
			$cc = "pengqiao.geng@zhaopin.com.cn";
			$emailHtml = $this->cemail->createHtml($email_items,$message,$button);
			if(!$this->comon_model->_sendEmail($to,$cc,'[智联安全运维平台]'.$title,$emailHtml)){
				return array('status' => -3);
			}else{
				return array('status' => 1);
			}
			 return array('status' => 1);
		}
		else{
		    $this->db->trans_rollback();
		     return array('status' => -2);
		}

	}

	/**
	 * 获得SEC事件的列表
	 * @return [type] [description]
	 */
	function srcAssignUsers_list($inputs)
	{

		$this->db->select('ZL_USER_NAME name');
		$zl_users = $this->db->get('zl_users');

		return array(
			'status' => 1,
			'data' => $zl_users->result_array()
		);
	}

	/**
	 * 获得SEC事件指派
	 * @return [type] [description]
	 */
	function srcAssign_push($inputs)
	{
		$session =  strtolower($this->session->userdata('username'));

		$data = array(
			'ZL_SRC_TIME' => $this->comon->_datetime(),
			'ZL_SRC_DEALUSER' => $inputs['dealuser'],
			'ZL_SRC_STATUS' => 2,
			'ZL_SRC_ASSIGNUSER' => $session

		);
		$data_process = array(
			'ZL_SRC_PROCESS_TIME' => $this->comon->_datetime(),
			'ZL_SRC_PROCESS_SRCID' => $inputs['id'],
			'ZL_SRC_PROCESS_USER' => $session,
			'ZL_SRC_CONTENT_APPEAL' => '安全（'.$session.'）已指派处理人，等待处理人（'.$inputs['dealuser'].'）接受</br>来自指派人（'.$session.'）的备注：'.$inputs['remarks']
		);

		$this->db->trans_begin();
		$this->db->where('ZL_SRC_ID',$inputs['id']);
		$zl_src_update = $this->db->update('zl_src',$data);
		$zl_src_process = $this->db->insert('zl_src_process',$data_process);

		if ($zl_src_update && $zl_src_process){
			$this->db->trans_commit();
			
			//发送邮件

			$this->db->select('ZL_USER_EMAIL');
			$this->db->where('ZL_USER_NAME',$inputs['dealuser']);
			$zl_users = $this->db->get('zl_users');
			$deal_mail= $zl_users->row()->ZL_USER_EMAIL;

			$this->db->select('ZL_SRC_PUSHUSER ,ZL_SRC_NAME');
			$this->db->where('ZL_SRC_ID',$inputs['id']);
			$zl_src = $this->db->get('zl_src');
			$this->db->select('ZL_USER_EMAIL');
			$this->db->where('ZL_USER_NAME',$zl_src->row()->ZL_SRC_PUSHUSER);
			$zl_users = $this->db->get('zl_users');
			$push_mail = $zl_users->row()->ZL_USER_EMAIL;

			$to = "";
			if ($inputs['dealuser'] == $session) {
				$to = $deal_mail;
			}else{

				$this->db->select('ZL_USER_EMAIL ');
				$this->db->where('ZL_USER_NAME',$session);
				$zl_users = $this->db->get('zl_users');
				$assign_mail= $zl_users->row()->ZL_USER_EMAIL;

				$to = $deal_mail.','.$assign_mail;
			}
			$cc = $push_mail;

			$title = 'SRC事件 - 处理人指派';
			$message = '见信好，安全（'.$session.'）指派处理人（'.$inputs['dealuser'].'）';
			
			$url_address = 'http://'.$_SERVER['HTTP_HOST'].base_url('index.php/main?path=src');
			$button = '<a class="my-btn" href="'.$url_address.'" style="width:150px;">去安全平台看看</a>';
			if ($inputs['remarks'] == null) {
				$inputs['remarks'] = "无";
			}
			$email_items = array(
				'SRC事件ID' => $inputs['id'],
				'工单名称' => $zl_src->row()->ZL_SRC_NAME,
				'状态' => "安全已指派处理人，等待处理人接受",
				'来自指派人（'.$session.'）的备注' => $inputs['remarks']
			);
			$to = "pengqiao.geng@zhaopin.com.cn";
			$cc = "pengqiao.geng@zhaopin.com.cn";
			$emailHtml = $this->cemail->createHtml($email_items,$message,$button);
			if(!$this->comon_model->_sendEmail($to,$cc,'[智联安全运维平台]'.$title,$emailHtml)){
				return array('status' => -3);
			}else{
				return array('status' => 1);
			}	

		}
		else{
		    $this->db->trans_rollback();
		     return array('status' => -2);
		}


	}

	/**
	 * 获得SEC事件删除
	 * @return [type] [description]
	 */
	function src_del($inputs)
	{

		$this->db->where('ZL_SRC_PROCESS_SRCID',$inputs['id']);
		$this->db->delete('zl_src_process');

		$this->db->where('ZL_SRC_ATTR_CONTENT_SRCID',$inputs['id']);
		$this->db->delete('zl_src_attr_content');

		$this->db->where('ZL_SRC_ATTR_SRCID',$inputs['id']);
		$this->db->delete('zl_src_attr');

		$this->db->where('ZL_SRC_ID',$inputs['id']);
		$this->db->delete('zl_src');

		return array('status' => 1);
	}

	/**
	 * 获得SEC事件删除
	 * @return [type] [description]
	 */
	function srcPushView_list($inputs)
	{

		$this->db->select('ZL_SRC_STATUS status');
		$this->db->where('ZL_SRC_ID',$inputs['id']);
		$zl_src = $this->db->get('zl_src');
		if ($zl_src->num_rows() <= 0) {
			return array('status' => -1);
		}

		return array(
			'status' => 1,
			'data' => $zl_src->result_array()
		);
	}

	/**
	 * 获得SEC事件详情
	 * @return [type] [description]
	 */
	function src_read($inputs)
	{

		$this->db->select('ZL_SRC_ID id,ZL_SRC_TIME time,ZL_SRC_NAME name, ZL_SRC_TYPE type,ZL_SRC_PUSHUSER pushuser,ZL_SRC_DEALUSER dealuser,ZL_SRC_ASSIGNUSER assiuser,ZL_SRC_STATUS status');
		$this->db->where('ZL_SRC_ID',$inputs['id']);
		$zl_src = $this->db->get('zl_src');
		if ($zl_src->num_rows() <= 0) {
			return array('status' => -1);
		}

		$this->db->select('ZL_SRC_ATTR_KEY attr_key,ZL_SRC_ATTR_VALUE attr_value');
		$this->db->where('ZL_SRC_ATTR_SRCID',$inputs['id']);
		$zl_src_attr = $this->db->get('zl_src_attr');

		$this->db->select('ZL_SRC_ATTR_CONTENT_KEY content_key,ZL_SRC_ATTR_CONTENT_VALUE content_value');
		$this->db->where('ZL_SRC_ATTR_CONTENT_SRCID',$inputs['id']);
		$zl_src_attr_content = $this->db->get('zl_src_attr_content');

		$this->db->select('ZL_SRC_PROCESS_TIME times,ZL_SRC_PROCESS_USER users,ZL_SRC_CONTENT_APPEAL appeal');
		$this->db->where('ZL_SRC_PROCESS_SRCID',$inputs['id']);
		$this->db->order_by('ZL_SRC_PROCESS_TIME','DESC');
		$zl_src_process = $this->db->get('zl_src_process');

		return array(
			'status' => 1,
			'data' => array(
				'src' => $zl_src->result_array(), 
				'attr'=> $zl_src_attr->result_array(), 
				'content' => $zl_src_attr_content->result_array(), 
				'process' => $zl_src_process->result_array()

			)
		);
	}

	/**
	 * 查询用户是否使我们冻结的
	 * @return [type] [description]
	 */
	function srcFrozenIPStatus_read($inputs)
	{
		$host = $this->config->item('APIHost');
		$url='http://'.$host['frozenIPStatus'].'/work-order/check-freeze-operator/'.$inputs['qID'].'/'.$inputs['zName'];
		return file_get_contents($url);
	}

	/**
	 * 查询公司信息
	 * @return [type] [description]
	 */
	function srcFrozenInfo_read($inputs)
	{
		$host = $this->config->item('APIHost');
		$url='http://'.$host['frozenInfo'].'/work-order/company-information/'.$inputs['qID'].'/'.$inputs['zName'].'?query='.$inputs['source'];
		return file_get_contents($url);
	}

	/**
	 * 解冻
	 * @return [type] [description]
	 */
	function srcOpenFrozen_push($inputs)
	{
		$host = $this->config->item('APIHost');
		$url='http://'.$host['frozen'].'/work-order/freeze-account/'.$inputs['qID'].'/'.$inputs['zID'].'?status='.$inputs['isOK'].'&source='.$inputs['source'];
		return file_get_contents($url);
	}

	

}

?>
